package cn.opopopo.design_pattern_learn.行为型.中介者.demo2;

import cn.opopopo.design_pattern_learn.行为型.中介者.demo1.Colleague;
import cn.opopopo.design_pattern_learn.行为型.中介者.demo1.Mediator;

import java.util.ArrayList;
import java.util.List;

// 房屋中介系统
public class HouseMediator extends Mediator {
    private List<HouseOwner> houseOwners;
    private List<Tenant> tenants;
    
    public HouseMediator() {
        this.houseOwners = new ArrayList<>();
        this.tenants = new ArrayList<>();
    }
    
    public void addHouseOwner(HouseOwner owner) {
        houseOwners.add(owner);
    }
    
    public void addTenant(Tenant tenant) {
        tenants.add(tenant);
    }
    
    @Override
    public void addUser(Colleague colleague) {
        // 这个方法在这个例子中不使用
    }
    
    @Override
    public void sendMessage(String message, Colleague sender) {
        if (sender instanceof HouseOwner) {
            // 房主发布房源信息，通知所有租客
            HouseOwner owner = (HouseOwner) sender;
            System.out.println("--- 房屋中介系统正在匹配租客 ---");
            for (Tenant tenant : tenants) {
                // 简单的价格匹配逻辑
                for (House house : owner.getHouses()) {
                    if (house.getPrice() <= tenant.getMaxPrice()) {
                        tenant.receiveMessage("发现匹配房源: " + house.getAddress() + 
                                            "，价格: " + house.getPrice(), owner.name);
                    }
                }
            }
        } else if (sender instanceof Tenant) {
            // 租客发布需求，通知所有房主
            Tenant tenant = (Tenant) sender;
            System.out.println("--- 房屋中介系统正在匹配房源 ---");
            for (HouseOwner owner : houseOwners) {
                owner.receiveMessage("有租客需求，预算: " + tenant.getMaxPrice(), tenant.name);
            }
        }
    }
}